import { http } from '@/utils/http'
import { useUserStore } from '@/stores'

/**
 * 科普视频
 */
export const getVideoListAPI = (params) => {
  return http({
    method: 'GET',
    url: '/course',
    data: params,
  })
}

export const createComment = (params) => {
  return http({
    method: 'POST',
    url: `/course/comment`,
    data: params,
  })
}

export const deleteCommentAPI = (id) => {
  return http({
    method: 'DELETE',
    url: `/course/comment/${id}`,
  })
}

export const getVideoInfoAPI = (id) => {
  return http({
    method: 'GET',
    url: '/course/info',
    data: { id },
  })
}

export const getMyVideoAPI = (auditStatus) => {
  return http({
    method: 'GET',
    url: '/expert/course',
    data: { auditStatus },
  })
}

export const uploadBigFile = (tempPath) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: '/video/upload',
      filePath: tempPath,
      name: 'file',
      success(res) {
        // 状态码 2xx，参考 axios 的设计
        const result = JSON.parse(res.data)
        if (res.statusCode >= 200 && res.statusCode < 300) {
          if (result.code === 200) {
            return resolve(result)
          }
          if (result.code >= 2000 && result.code <= 2011) {
            //账号问题
            //清理用户信息，跳转到登录页
            const userStore = useUserStore()
            userStore.removeToken()
            userStore.setUser({})
            uni.showToast({
              icon: 'none',
              title: result.msg || '请求错误',
            })
            setTimeout(() => {
              uni.navigateTo({ url: '/pages/login/login' }) //关闭当前页面 重定向到登录页
            }, 1500)
          }
          //其他问题 直接提示框告知用户即可
          uni.showToast({
            icon: 'none',
            title: result.msg || '请求错误',
          })
          return reject(res)
        } else {
          // 其他错误 -> 根据后端错误信息轻提示
          uni.showToast({
            icon: 'none',
            title: result.msg || '请求错误',
          })
          return reject(res)
        }
      },
      // 响应失败
      fail(err) {
        uni.showToast({
          icon: 'none',
          title: '网络错误，换个网络试试',
        })
        return reject(err)
      },
    })
  })
}

export const saveCourse = (courseInfoDto) => {
  return http({
    method: 'PUT',
    url: '/expert/course',
    data: courseInfoDto,
  })
}

export const getSingleCourseInfo = (courseId, index) => {
  return http({
    method: 'GET',
    url: `/expert/course/${courseId}/${index}`,
  })
}
export const reEditCourse = (courseId) => {
  return http({
    method: 'DELETE',
    url: `/expert/course/reEdit/${courseId}`,
  })
}
export const submitCourse = (courseInfoDto) => {
  return http({
    method: 'PUT',
    url: `/expert/course/submit`,
    data: courseInfoDto,
  })
}

export const uploadCourseAPI = (courseInfoDto) => {
  return http({
    method: 'POST',
    url: '/expert/course/upload',
    data: courseInfoDto,
  })
}

export const getMyPublishCourse = () => {
  return http({
    method: 'GET',
    url: '/expert/publishCourse',
  })
}

export const downCourse = (courseId) => {
  return http({
    method: 'DELETE',
    url: `/expert/course/down/${courseId}`,
  })
}

export const upCourse = (courseId) => {
  return http({
    method: 'POST',
    url: `/expert/course/up/${courseId}`,
  })
}

export const deleteCourse = (id) => {
  return http({
    method: 'DELETE',
    url: `/expert/course/${id}`,
  })
}

export const getPostObjectParamsAPI = () => {
  return http({
    method: 'GET',
    url: 'http://localhost:8081/video/getSign',
  })
}
